package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.project.system.domain.UserAccount;
import com.happy.cat.project.system.domain.UserLogCard;
import com.happy.cat.project.system.service.IUserAccountService;
import com.happy.cat.project.system.service.IUserLogCardService;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户购买月卡日志Controller
 *
 * @author verne
 * @date 2020-08-15
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/userLogCard" )
@Api("用户购买月卡日志 API")
public class UserLogCardController extends BaseController {

    private final IUserLogCardService iUserLogCardService;

    private final IUserAccountService userAccountService;

    /**
     * 查询用户购买月卡日志列表
     */
    @ApiOperation(value = "查询用户购买月卡日志列表",notes = "查询用户购买月卡日志列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:userLogCard:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserLogCard userLogCard)    {

        UserAccount user = null;

        if(StringUtils.isNotEmpty(userLogCard.getUserKey())){
            LambdaQueryWrapper<UserAccount> lb =
                    Wrappers.lambdaQuery(UserAccount.class)
                            .and(wrapper->
                                    wrapper.like(UserAccount::getNick, "%" + userLogCard.getUserKey() + "%")
                                            .or()
                                            .like(UserAccount::getAccountName,"%" + userLogCard.getUserKey() + "%")
                                            .or()
                                            .like(UserAccount::getCatCode,"%" + userLogCard.getUserKey() + "%"));
            user = userAccountService.getOne(lb);
        }

        startPage();
        LambdaQueryWrapper<UserLogCard> lqw = new LambdaQueryWrapper<UserLogCard>();
        if (userLogCard.getCardType() != null){
            lqw.eq(UserLogCard::getCardType ,userLogCard.getCardType());
        }
        if (userLogCard.getUserId() != null){
            lqw.eq(UserLogCard::getUserId ,userLogCard.getUserId());
        }
        if (userLogCard.getCatFoot() != null){
            lqw.eq(UserLogCard::getCatFoot ,userLogCard.getCatFoot());
        }
        if (userLogCard.getExpireTime() != null){
            lqw.eq(UserLogCard::getExpireTime ,userLogCard.getExpireTime());
        }
        if(user!=null){
            lqw.eq(UserLogCard::getUserId ,user.getId());
        }
        List<UserLogCard> list = iUserLogCardService.list(lqw);

        if(list.size()>0){
            Set<Long> uids = list.stream().map(UserLogCard::getUserId).collect(Collectors.toSet());
            Map<Long, UserAccount> userMap = userAccountService.listByIds(uids).stream().collect(Collectors.toMap(UserAccount::getId, e -> e));
            list.forEach(e -> {
                e.setUserAccount(userMap.get(e.getUserId()));
            });
        }
        return getDataTable(list);
    }

    /**
     * 导出用户购买月卡日志列表
     */
    @ApiOperation(value = "导出用户购买月卡日志列表",notes = "导出用户购买月卡日志列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userLogCard:export')" )
    @Log(title = "用户购买月卡日志" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(UserLogCard userLogCard) {
        LambdaQueryWrapper<UserLogCard> lqw = new LambdaQueryWrapper<UserLogCard>(userLogCard);
        List<UserLogCard> list = iUserLogCardService.list(lqw);
        ExcelUtil<UserLogCard> util = new ExcelUtil<UserLogCard>(UserLogCard. class);
        return util.exportExcel(list, "userLogCard" );
    }

    /**
     * 获取用户购买月卡日志详细信息
     */
    @ApiOperation(value = "获取用户购买月卡日志详细信息",notes = "获取用户购买月卡日志详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userLogCard:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iUserLogCardService.getById(id));
    }

    /**
     * 新增用户购买月卡日志
     */
    @ApiOperation(value = "新增用户购买月卡日志",notes = "新增用户购买月卡日志",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userLogCard:add')" )
    @Log(title = "用户购买月卡日志" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserLogCard userLogCard) {
        return toAjax(iUserLogCardService.save(userLogCard) ? 1 : 0);
    }

    /**
     * 修改用户购买月卡日志
     */
    @ApiOperation(value = "修改用户购买月卡日志",notes = "修改用户购买月卡日志",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userLogCard:edit')" )
    @Log(title = "用户购买月卡日志" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserLogCard userLogCard) {
        return toAjax(iUserLogCardService.updateById(userLogCard) ? 1 : 0);
    }

    /**
     * 删除用户购买月卡日志
     */
    @ApiOperation(value = "删除用户购买月卡日志",notes = "删除用户购买月卡日志",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userLogCard:remove')" )
    @Log(title = "用户购买月卡日志" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iUserLogCardService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
